package com.itheima.controller;

import com.itheima.domain.pojo.Emp;
import com.itheima.domain.query.QueryParam;
import com.itheima.domain.vo.PageResult;
import com.itheima.domain.vo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:篍
 * @Date: 2025/07/11 15:15
 * @Description:接收请求，响应Result
 */
@RestController
@RequestMapping("/emps")
@Slf4j
@CrossOrigin //跨源
public class EmpController {
    @Autowired
    EmpService empService;

    /**
     * 员工分页查询
     * @return
     */
    @GetMapping
    public Result empPage(QueryParam empQueryParam){
        log.info("empQueryParam={}",empQueryParam);
        PageResult<Emp> empPage =empService.empPage(empQueryParam);
        return Result.success(empPage);
    }

    @PostMapping
    public Result empAdd(@RequestBody Emp emp){
        log.warn("emp={}",emp);
        empService.addEmpAndExpr(emp);
        return Result.success();
    }

    /**列出所有员工
     *
     * @return员工集合
     */
    @GetMapping("/list")
    public Result findAllEmp(){
        List<Emp> emps =empService.findAllEmp();
        return Result.success(emps);
    }


    @DeleteMapping
    public Result deleteByIds(Integer[] ids) throws Exception {
        empService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result findEmpById(@PathVariable("id") Integer id) {
        Emp emp=empService.findEmpById(id);
        log.warn("emp={}",emp);
        return Result.success(emp);
    }

    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        empService.updateEmp(emp);
        return Result.success();
    }
}
